//
//  LSZMVDetailsViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/9/5.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZMVDetailsViewController.h"

@interface LSZMVDetailsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong) UICollectionView * collectionViewDetails;
@property(nonatomic,strong) NSMutableArray * mutArrayList;
@property(nonatomic,assign) NSInteger page;
@end

@implementation LSZMVDetailsViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.collectionViewDetails reloadData];
}
-(void)initGetDetails:(NSInteger)page
{
    [LSZRequestResolutionDic getListOfAllTheMVTypes:self.type album_id:@"" artist_id:@"" key_word:@"" page:[NSString stringWithFormat:@"%ld",(long)page] :^(BOOL isOK, NSString * str, LSZListOfMVTypes * manager) {
        if (isOK == YES) {
            if (page == 1) {
                [self.mutArrayList removeAllObjects];
                [self.mutArrayList addObjectsFromArray:manager.arrayData];
            }else{
                [self.mutArrayList addObjectsFromArray:manager.arrayData];
            }
            [self.collectionViewDetails reloadData];
        }else{
            [self initWithtring:str];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.mutArrayList = [[NSMutableArray alloc]init];
    self.page = 1;
    [self initGetDetails:self.page];

    [self setDetailsUI];

    //表的上下拉
    self.collectionViewDetails.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.page = 1;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格  和结束刷新
            [self initGetDetails:self.page];
            [self.collectionViewDetails.mj_header endRefreshing];
        });
    }];
    // 上拉刷新
    self.collectionViewDetails.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.page++;
        //赞美诗的排行榜
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格  和结束刷新
            [self initGetDetails:self.page];
            [self.collectionViewDetails.mj_footer endRefreshing];
        });
    }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mutArrayList.count;
}
#pragma mark - 单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LSZMVSongCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LSZMVSongCollectionViewCell" forIndexPath:indexPath];
    if (self.mutArrayList.count > indexPath.row) {
        LSZListOfMVTypesMg * mg = nil;
        mg = self.mutArrayList[indexPath.row];
        [cell.imageViewCover sd_setImageWithURL:[NSURL URLWithString:mg.cover_url] placeholderImage:[UIImage imageNamed:@"placeholder"]];

        if ([self.type isEqualToString:@"1"]) {
            cell.labelTitle.text = mg.name;
            cell.labelNote.text = [NSString stringWithFormat:@"%@-%@",mg.artist_name,mg.album_name];
            cell.imageViewPlay.hidden = NO;
            cell.labelTime.text = mg.play_time;
            cell.labelTime.textAlignment = NSTextAlignmentRight;
            cell.imageViewRightPlay.hidden = YES;
            cell.labelHits.text = [LSZSpecialPackaging theNumberViewsForTheAlbumAhanges:mg.listens];
            cell.imageViewTimePlayer.hidden = NO;

            CGFloat size = [LSZSpecialPackaging initWithReturnsthesizeOf:12.5 :0.5];
            CGFloat width = [LSZSpecialPackaging caculateWidthOfText:cell.frame.size.width-15 height:cell.labelTime.frame.size.height font:size :cell.labelTime.text]-15;

            CGRect rectTime = cell.labelTime.frame;
            rectTime.size.width = width;
            rectTime.origin.x = cell.frame.size.width-width-5;
            cell.labelTime.frame = rectTime;

            CGRect rect = cell.imageViewTimePlayer.frame;
            rect.origin.x = CGRectGetMinX(cell.labelTime.frame)-2-cell.imageViewTimePlayer.frame.size.width;
            cell.imageViewTimePlayer.frame = rect;

        }else{
            cell.labelTitle.text = mg.name;
            cell.imageViewPlay.hidden = YES;
            cell.labelTime.textAlignment = NSTextAlignmentLeft;
            cell.albumHaveSongsNum.text = cell.labelTime.text = [NSString stringWithFormat:@"共%@首",mg.total_song];
            cell.imageViewRightPlay.hidden = NO;
            cell.labelHits.text = [LSZSpecialPackaging theNumberViewsForTheAlbumAhanges:mg.hits];
            cell.imageViewTimePlayer.hidden = YES;
            if ([self.type isEqualToString:@"2"]){
                cell.labelNote.text = mg.artist_name;
            }else{
                 CGRect rect = cell.labelTitle.frame;
                rect.origin.y = rect.origin.y+8;
                cell.labelTitle.frame = rect;
            }
        }

        CGFloat size = [LSZSpecialPackaging initWithReturnsthesizeOf:14 :0.5];
        CGFloat width = [LSZSpecialPackaging caculateWidthOfText:cell.frame.size.width-15 height:cell.labelHits.frame.size.height font:size :cell.labelHits.text]-15;
        CGRect rectlabelHits = cell.labelHits.frame;
        rectlabelHits.origin.x = cell.frame.size.width-width-5;
        rectlabelHits.size.width = width;
        cell.labelHits.frame = rectlabelHits;
        CGRect rectImageViewHits = cell.imageViewHits.frame;
        rectImageViewHits.origin.x = CGRectGetMinX(cell.labelHits.frame)-cell.imageViewHits.frame.size.width-2;
        cell.imageViewHits.frame = rectImageViewHits;
    }
    if ([cell.albumHaveSongsNum.text isEqualToString:@""]) {
        cell.albumHaveSongsNum.hidden = true;
    }else{
        cell.albumHaveSongsNum.hidden = false;
    }
    if ([self.type isEqualToString:@"1"]) {
        cell.labelTime.hidden = false;
    }else{
        cell.labelTime.hidden = true;
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.mutArrayList.count > indexPath.row) {
        LSZListOfMVTypesMg * mg = nil;
        mg = self.mutArrayList[indexPath.row];
        if ([self.type isEqualToString:@"1"]) {
            LSZVideoPlaybackInterfaceVC * VC  = [[LSZVideoPlaybackInterfaceVC alloc]init];
            VC.lastMg = mg;
            mg.listens = [NSString stringWithFormat:@"%ld",(long)[mg.listens integerValue]+1];
            [self.navigationController pushViewController:VC animated:YES];
        }else{
            LSZAlbumSingerViewController * vc = [[LSZAlbumSingerViewController alloc]init];
            vc.type = self.type;
            vc.lastMg = mg;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}
-(void)setDetailsUI
{
    CGFloat width = (ScreenWidth - 5*3-1) /2;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    NSLog(@"width/3 = %f", width/3);
    layout.itemSize = CGSizeMake(width,width*9/16 + 50);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5,0,5);

    self.collectionViewDetails = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight-44) collectionViewLayout:layout];
    self.collectionViewDetails.delegate   = self;
    self.collectionViewDetails.dataSource = self;
    self.collectionViewDetails.backgroundColor = RGBColor(236, 236, 236);
    [self.view addSubview:self.collectionViewDetails];
    [self.collectionViewDetails registerClass:[LSZMVSongCollectionViewCell class] forCellWithReuseIdentifier:@"LSZMVSongCollectionViewCell"];
}
@end
